<?php

namespace app\admin\controller;

use app\BaseController;
use think\facade\Db; // 数据库的链接
use app\Upload;

class Img extends BaseController
{
    // 删除图片
    function deleteImg()
    {
        $get = input();
        Db::name('file')->where('id', $get['id'])->delete();
        return array('code' => 200, 'msg' => "删除图片成功!");
    }
    // 获取照片列表 
    function getImglist()
    {
        $_P = input();
        // 处理分页
        $pageSize = $_P['pageSize'] > 100 ? 100 : $_P['pageSize'];
        $page = $_P['pageNo'];


        $array = Db::name('file')
            ->where(@$sql)
            ->field("*")
            ->order(['id' => 'desc'])
            ->paginate([
                'list_rows' => $pageSize,
                'page'     => $page,
            ])
            ->toArray();
        $count = Db::name('file')->where(@$sql)->count();
        return array('code' => 200, 'data' => array('list' => $array['data'], 'total' => $array['total'], 'count' => $count));
    }
    
    /**
     * 上传图片
     */
    public function uploadimg()
    {
        $_P = input();
        $Upload = new Upload;
        $img = $Upload->fileImg($_P);
        // 获取当前域名
        $url = $_SERVER['HTTP_HOST'];
        $url = 'http://' . $url . '/';
        // 保存图片信息到数据库 后台上传
        if (@$_P['type'] == 'admin') {
            $data['url'] = $url.$img;
            $data['create_time'] = time();
            $id = Db::name('file')->insertGetId($data);
        }
       
        return json(array('code' => 200, 'errno' => 0, 'data' => array('url' => $url.$img)));
    }
}
